package com.ghh.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.dao.RoleDao;
import com.ghh.pojo.Role;
import com.ghh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 角色管理服务
 *
 * */

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;


    //角色查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Role> page = roleDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Integer[] permissionIds,Integer[] menuIds,Role role) {
        roleDao.add(role);//角色基本信息
        Integer roleId = role.getId();
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("permission_id", permissionId);
                map.put("role_id", roleId);
                roleDao.setPermissionAndRole(map);//角色-权限关联信息
            }
        }
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map2 = new HashMap<>();
                map2.put("menu_id", menuId);
                map2.put("role_id", roleId);
                roleDao.setMenuAndRole(map2);//角色-菜单关联信息
            }
        }
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }


    @Override
    public void edit(Integer[] permissionIds,Integer[] menuIds,Role role) {

        roleDao.edit(role);//修改基本信息
        roleDao.deleteRoleAndPermissionByRoleId(role.getId());//先删除关联表数据
        //再把新编辑的关联ID添加到关联表
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer integer : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("permission_id", integer);
                map.put("role_id", role.getId());
                roleDao.setPermissionAndRole(map);
            }
        }
        roleDao.deleteRoleAndMenuByRoleId(role.getId());//先删除关联表数据
        //再把新编辑的关联ID添加到关联表
        if (menuIds != null && menuIds.length > 0) {
            for (Integer integer : menuIds) {
                Map<String, Integer> map2 = new HashMap<>();
                map2.put("menu_id", integer);
                map2.put("role_id", role.getId());
                roleDao.setPermissionAndRole(map2);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        roleDao.deleteRoleAndPermissionByRoleId(id);//rolr-permission关联表
        roleDao.deleteRoleAndMenuByRoleId(id);//rolr-menu关联表
        roleDao.deleteByRoleId(id);//role基本信息
    }
}
